var parentData, parentActive;
var dateFormatStr = 'yyyy-MM-dd HH:mm:ss';

layui.use(['form', 'table', 'layer', 'laydate','soulTable'], function () {
	var form = layui.form,
		layer = layui.layer,
		table = layui.table,
		soulTable = layui.soulTable
		$ = layui.$;
	
	window.active = {
		addPrize: () => {
			popEdit(parentData, 'page/bid/addPrize.html', ["550px", "670px"]);
		},
		// edit:(data)=>{
		// 	popEdit(data, 'page/bid/addPrize.html',["600px", "610px"]);
		// },
		reload: () => {
			table.render({
				elem: '#tableList',
				url:  baseUrl('auction/prize'),
				where: {
					id: parentData.id //搜索的关键字
				},
				toolbar: true,
				height: 430,
				toolbar:'#tableListHeadBar',
				defaultToolbar:[],
				rowDrag: { done: function(obj) {
				    // 完成时（松开时）触发
				    // 如果拖动前和拖动后无变化，则不会触发此方法
				    // console.log(obj.row) // 当前行数据
				    // console.log(obj.cache) // 改动后全表数据
				    // console.log(obj.oldIndex) // 原来的数据索引
					// console.log(obj.newIndex) // 改动后数据索引
					var	auction_id=parentData.id
					   obj.cache=obj.cache.map((v,i)=>{
							v.sort=i+1
							return v
						})
						sort=obj.row.sort
						auction_goods_id = obj.row.auction_goods_id
						security=obj.row.security
						goods_id=obj.row.goods_id
					ajaxAsyncNoTip('auction/add_prize', {auction_goods_id,sort,auction_id,security,goods_id},'POST')
				}},
			   cols: [[
					{width:300,field:'name', title: '商品名称'},
					{width:100,templet: '#templetZT', title: '状态'},
					{width:120,field:'security', title: '商品保证金'},
					{width:90,field:'view', title: '商品热度'},
					{width:120, templet: '#tableListBar', title: '操作'}
				]]
				,done: function () {
				    soulTable.render(this)
				}
			});
		},
		del: (data) => {
			var auction_goods_id=data.auction_goods_id
			popDelete(data.name, 'auction/prize', {auction_goods_id});
		},
	};
	//操作
	$('.layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this,parentData) : '';

	});
	//表格头部按钮操作
	table.on('toolbar(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
		
	});
	//表格按钮操作
	table.on('tool(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data, obj) : '';
	});

	form.on("submit(save)",element=>{
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(index); //再执行关闭
		return false
	})
	//延时初始化，确保数据已经塞进来了
	setTimeout(() => {
		if (!parentData) return;
		//初始化数据
		form.val("dataform", JSON.parse(JSON.stringify(parentData)));
		//加载表格
		active.reload();
	}, 200);

});